# -*- coding: utf-8 -*-
"""
@author: Alterado por Bruno Figueira 11234
@author: Alterado por Luis Pereira 12784
@last updated: 25-11-2014
@obs: Modulo que cria a interface gráfica através do wxGlade;
"""

import sys
import wx
from controller.controller import Controller

# begin wxGlade: extracode
# end wxGlade


class MyFrame(wx.Frame):
    def __init__(self, *args, **kwds):
        # begin wxGlade: MyFrame.__init__
        self.controller = Controller()
        kwds["style"] = wx.DEFAULT_FRAME_STYLE
        wx.Frame.__init__(self, *args, **kwds)

        # Menu Bar
        self.menu_principal = wx.MenuBar()
        self.file = wx.Menu()
        self.quit = wx.MenuItem(self.file, 11, "Quit", "", wx.ITEM_NORMAL)
        self.file.AppendItem(self.quit)
        self.menu_principal.Append(self.file, "File")
        self.database = wx.Menu()
        self.load_data = wx.MenuItem(self.database, 21, "Load Data from XLS",
                                     "", wx.ITEM_NORMAL)
        self.database.AppendItem(self.load_data)
        self.menu_principal.Append(self.database, "Database")
        self.stats = wx.Menu()
        self.ipc_stats = wx.MenuItem(self.stats, 31, "Show IPC Global stats",
                                     "", wx.ITEM_NORMAL)
        self.stats.AppendItem(self.ipc_stats)
        self.taxa_stats = wx.MenuItem(self.stats, 32,
                                      u"Show Taxa Inflacção stats", "",
                                      wx.ITEM_NORMAL)
        self.stats.AppendItem(self.taxa_stats)
        self.remun_min_stats = wx.MenuItem(self.stats, 33,
                                           u"Show Remunerações Mínimas stats",
                                           "", wx.ITEM_NORMAL)
        self.stats.AppendItem(self.remun_min_stats)
        self.remun_max_stats = wx.MenuItem(self.stats, 34,
                                           u"Show Remunerações Máximas stats",
                                           "", wx.ITEM_NORMAL)
        self.stats.AppendItem(self.remun_max_stats)
        self.evolucao_remun_stats = wx.MenuItem(self.stats, 35,
                                                u"Show Evoluçao Remunerações Stats",
                                                "", wx.ITEM_NORMAL)
        self.stats.AppendItem(self.evolucao_remun_stats)
        self.pib_stats = wx.MenuItem(self.stats, 36, "Show PIB stats", "",
                                     wx.ITEM_NORMAL)
        self.stats.AppendItem(self.pib_stats)
        self.rend_nacional_stats = wx.MenuItem(self.stats, 37,
                                               "Show Rendimento Nacional Anual Bruto stats",
                                               "", wx.ITEM_NORMAL)
        self.stats.AppendItem(self.rend_nacional_stats)
        self.rend_disponivel_stats = wx.MenuItem(self.stats, 38,
                                                 "Show Rendimento Disponivel Anual Bruto stats",
                                                 "", wx.ITEM_NORMAL)
        self.stats.AppendItem(self.rend_disponivel_stats)
        self.remun_anuais_stats = wx.MenuItem(self.stats, 39,
                                              u"Show Remunerações per Capita Anuais",
                                              "", wx.ITEM_NORMAL)
        self.stats.AppendItem(self.remun_anuais_stats)
        self.menu_principal.Append(self.stats, "Stats")
        self.SetMenuBar(self.menu_principal)
        # Menu Bar end

        self.__set_properties()
        self.__do_layout()

        self.Bind(wx.EVT_MENU, self.evt_quit, self.quit)
        self.Bind(wx.EVT_MENU, self.evt_load_data, self.load_data)
        self.Bind(wx.EVT_MENU, self.evt_show_ipc, self.ipc_stats)
        self.Bind(wx.EVT_MENU, self.evt_show_taxa, self.taxa_stats)
        self.Bind(wx.EVT_MENU, self.evt_show_remun_min,
                  self.remun_min_stats)
        self.Bind(wx.EVT_MENU, self.evt_show_remun_max,
                  self.remun_max_stats)
        self.Bind(wx.EVT_MENU, self.evt_show_evolucao,
                  self.evolucao_remun_stats)
        self.Bind(wx.EVT_MENU, self.evt_show_pib, self.pib_stats)
        self.Bind(wx.EVT_MENU, self.evt_show_rend_nacional,
                  self.rend_nacional_stats)
        self.Bind(wx.EVT_MENU, self.evt_show_rend_disponivel,
                  self.rend_disponivel_stats)
        self.Bind(wx.EVT_MENU, self.evt_show_remun_anuais,
                  self.remun_anuais_stats)
        # end wxGlade

    def __set_properties(self):
        # begin wxGlade: MyFrame.__set_properties
        self.SetTitle("IPC_Portugal")
        # end wxGlade

    def __do_layout(self):
        # begin wxGlade: MyFrame.__do_layout
        sizer_1 = wx.BoxSizer(wx.VERTICAL)
        self.SetSizer(sizer_1)
        sizer_1.Fit(self)
        self.Layout()
        # end wxGlade

    def evt_quit(self, event):  # wxGlade: MyFrame.<event_handler>
        sys.exit(0)
        event.Skip()

    def evt_load_data(self, event):  # wxGlade: MyFrame.<event_handler>
        openFileDialog = wx.FileDialog(self, "Open XLS file", "", "",
                                       "XLS files (*.xls)|*.xls", wx.FD_OPEN
                                       | wx.FD_FILE_MUST_EXIST)

        if openFileDialog.ShowModal() == wx.ID_CANCEL:
            return     # the user changed idea...

        self.controller.populate_database_from_excel(openFileDialog.GetPath())
        wx.MessageBox('Your XLS has been succesfully read', 'Info', wx.OK
                      | wx.ICON_INFORMATION)
        event.Skip()

    def evt_show_ipc(self, event):  # wxGlade: MyFrame.<event_handler>
        yearDialog = YearDialog()
        if yearDialog.ShowModal() == wx.ID_OK:
            if self.controller.use_plot_graphics(Controller.STAT_IPC,
                                    int(yearDialog.startyear.GetValue()),
                                    int(yearDialog.endyear.GetValue())) == 1:
                self.show_no_data_message()
        event.Skip()

    def evt_show_taxa(self, event):  # wxGlade: MyFrame.<event_handler>
        yearDialog = YearDialog()
        if yearDialog.ShowModal() == wx.ID_OK:
            if self.controller.use_plot_graphics(Controller.STAT_TAXA,
                                    int(yearDialog.startyear.GetValue()),
                                    int(yearDialog.endyear.GetValue())) == 1:
                self.show_no_data_message()
        event.Skip()

    def evt_show_remun_min(self, event):  # wxGlade: MyFrame.<event_handler>
        yearDialog = YearDialog()
        if yearDialog.ShowModal() == wx.ID_OK:
            if self.controller.use_plot_graphics(Controller.STAT_REMUN_MIN,
                                    int(yearDialog.startyear.GetValue()),
                                    int(yearDialog.endyear.GetValue())) == 1:
                self.show_no_data_message()
        event.Skip()

    def evt_show_remun_max(self, event):  # wxGlade: MyFrame.<event_handler>
        yearDialog = YearDialog()
        if yearDialog.ShowModal() == wx.ID_OK:
            if self.controller.use_plot_graphics(Controller.STAT_REMUN_MAX,
                                    int(yearDialog.startyear.GetValue()),
                                    int(yearDialog.endyear.GetValue())) == 1:
                self.show_no_data_message()
        event.Skip()

    def evt_show_evolucao(self, event):  # wxGlade: MyFrame.<event_handler>
        yearDialog = YearDialog()
        if yearDialog.ShowModal() == wx.ID_OK:
            if self.controller.use_plot_graphics(Controller.STAT_EV,
                                    int(yearDialog.startyear.GetValue()),
                                    int(yearDialog.endyear.GetValue())) == 1:
                self.show_no_data_message()
        event.Skip()

    def evt_show_pib(self, event):  # wxGlade: MyFrame.<event_handler>
        yearDialog = YearDialog()
        if yearDialog.ShowModal() == wx.ID_OK:
            if self.controller.use_plot_graphics(Controller.STAT_PIB,
                                    int(yearDialog.startyear.GetValue()),
                                    int(yearDialog.endyear.GetValue())) == 1:
                self.show_no_data_message()
        event.Skip()

    def evt_show_rend_nacional(self, event):  # wxGlade: MyFrame.<event_handler>
        yearDialog = YearDialog()
        if yearDialog.ShowModal() == wx.ID_OK:
            if self.controller.use_plot_graphics(Controller.STAT_REND_NACIONAL,
                                    int(yearDialog.startyear.GetValue()),
                                    int(yearDialog.endyear.GetValue())) == 1:
                self.show_no_data_message()
        event.Skip()

    def evt_show_rend_disponivel(self, event):  # wxGlade: MyFrame.<event_handler>
        yearDialog = YearDialog()
        if yearDialog.ShowModal() == wx.ID_OK:
            if self.controller.use_plot_graphics(Controller.STAT_REND_DISPONIVEL,
                                    int(yearDialog.startyear.GetValue()),
                                    int(yearDialog.endyear.GetValue())) == 1:
                self.show_no_data_message()
        event.Skip()

    def evt_show_remun_anuais(self, event):  # wxGlade: MyFrame.<event_handler>
        yearDialog = YearDialog()
        if yearDialog.ShowModal() == wx.ID_OK:
            if self.controller.use_plot_graphics(Controller.STAT_REMUN_ANUAIS,
                                    int(yearDialog.startyear.GetValue()),
                                    int(yearDialog.endyear.GetValue())) == 1:
                self.show_no_data_message()
        event.Skip()

    def show_no_data_message(self):
        wx.MessageBox('There is no data to be shown.\nCheck if you loaded your'
                      + ' Database', 'Info', wx.OK
                      | wx.ICON_INFORMATION)


class YearDialog(wx.Dialog):
    """
    Classe que pergunta ao utilizador o startYear e o endYear. Código baseado
    naquele encontrado no seguinte site:
    http://python.dzone.com/articles/wxpython-how-create-login
    """
    def __init__(self):
        """Constructor"""
        wx.Dialog.__init__(self, None, title="start and end Year")

        start_year_sizer = wx.BoxSizer(wx.HORIZONTAL)

        start_year_lbl = wx.StaticText(self, label="StartYear:")
        start_year_sizer.Add(start_year_lbl, 0, wx.ALL|wx.CENTER, 5)
        self.startyear = wx.TextCtrl(self)
        start_year_sizer.Add(self.startyear, 0, wx.ALL, 5)

        end_year_sizer = wx.BoxSizer(wx.HORIZONTAL)

        p_lbl = wx.StaticText(self, label="EndYear:")
        end_year_sizer.Add(p_lbl, 0, wx.ALL|wx.CENTER, 5)
        self.endyear = wx.TextCtrl(self)
        end_year_sizer.Add(self.endyear, 0, wx.ALL, 5)

        main_sizer = wx.BoxSizer(wx.VERTICAL)
        main_sizer.Add(start_year_sizer, 0, wx.ALL, 5)
        main_sizer.Add(end_year_sizer, 0, wx.ALL, 5)

        btn = wx.Button(self, wx.ID_OK)
        main_sizer.Add(btn, 0, wx.ALL|wx.CENTER, 5)

        self.SetSizer(main_sizer)